/**
 * Copyright (C), 2018-2018, https://blog.fundodoo.com
 * Author:   醉探索戈壁
 * Date:     2018/7/11 下午7:27
 * Description: AthleteController
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.fundodoo.blog.aggrid.controller;

import com.fundodoo.blog.aggrid.model.Athlete;
import com.fundodoo.blog.aggrid.repositories.AthleteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 *
 * @author 醉探索戈壁
 * @create 2018/7/11 下午7:27
 * @since 1.0.0
 */
@CrossOrigin(origins = "http://localhost:4200")
@RestController
public class AthleteController {


    private final AthleteRepository athleteRepository;

    @Autowired
    public AthleteController(AthleteRepository athleteRepository){
        this.athleteRepository = athleteRepository;
    }

    @GetMapping("/athletes")
    public Iterable<Athlete> getAthletes() {
        return athleteRepository.findAll();
    }

    @GetMapping("/athlete")
    public Optional<Athlete> getAthlete(@RequestParam(value = "id") Long athleteId) {
        return athleteRepository.findById(athleteId);
    }

    @PostMapping("/saveAthlete")
    public Athlete saveAthlete(@RequestBody Athlete athlete) {
        return athleteRepository.save(athlete);
    }

    @PostMapping("/deleteAthlete")
    public void deleteAthlete(@RequestBody Long athleteId) {
        athleteRepository.deleteById(athleteId);
    }

}
